package com.pandora.credit.mongo.entity;

import com.pandora.common.util.VerifyUtil;
import com.pandora.credit.exception.InvalidDataException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author developer
 */
public class TrConvertReportFormat extends AbstractReportAfterFormat {
    @Override
    protected ReportMetaData getMetaDataByReport(AbstractReport report) {
        TrReport trReport = (TrReport)report;
        ReportMetaData metaData = new ReportMetaData();
        TrDataSource ds = trReport.getDataSource();
        String mobile;
        long reportId;
        if (ds != null) {
            mobile = ds.getMobile();
            if (VerifyUtil.isEmpty(mobile)) {
                logger.info("no mobile in the report");
                throw new InvalidDataException("报告中未找到用户手机信息!");
            }
            metaData.setMobile(mobile);

            reportId = trReport.getReportId();
            if (reportId <= 0) {
                logger.info("no report id in the report");
                throw new InvalidDataException("报告中未找到用报告id!");
            }
            metaData.setReportId(reportId);
        } else {
            logger.info("no data source in the report");
            throw new InvalidDataException("报告中未找到DataSource!");
        }
        return metaData;
    }

    @Override
    protected AbstractReport dealReportData(AbstractReport report) {
        TrReport wmReport = (TrReport)report;
        if (wmReport.getInvoiceData() != null) {
            wmReport.setInvoiceData(null);
        }
        List<HistorySummary> historySummaryList = new ArrayList<>();
        if (wmReport.getHistorySummaryList() != null) {
            for (HistorySummary summary: wmReport.getHistorySummaryList()) {
                if("月消费分析".equals(summary.getTitle())) {
                    historySummaryList.add(summary);
                }
            }
        }
        wmReport.setHistorySummaryList(historySummaryList);
        return wmReport;
    }
}
